import {
    startGapTask,
    getTaskStatus,
    startMineTask,
    downLoadMaps
} from '@/service/task';
import {
    message
} from 'antd';
import { saveAs } from 'file-saver';

const Model = {
    namespace: 'task',
    state: {
        scale: 0,
        taskId: null,
        taskStatus: 0,
        loading: true,
        current: 0
    },
    effects: {
        * startGapTask({ payload }, { put, call }) {
            const res = yield call(startGapTask, payload);
            if (res.code === 200) {
                console.log('payload.sampleid', payload);
                yield put({
                    type: 'maps/getInfo',
                    payload: payload.sampleId.pictureId
                })
                yield put({
                    type: 'saveTaskId',
                    payload: res.TaskId
                })
                yield put({
                    type: 'getTaskStatus',
                    payload: res.TaskId
                })
            }
        },
        * startMineTask({ payload }, { put, call }) {
            const res = yield call(startMineTask, payload)
            console.log('payload.sampleid', payload);
            if (res.code === 200) {
                yield put({
                    type: 'maps/getInfo',
                    payload: payload.sampleId.pictureId
                })
                yield put({
                    type: 'saveTaskId',
                    payload: res.TaskId
                })
                yield put({
                    type: 'getTaskStatus',
                    payload: res.TaskId
                })
            }
        },
        * getTaskStatus({ payload }, { put, call }) {
            const delay = (ms) => new Promise((resolve) => {
                setTimeout(resolve, ms);
            });
            yield call(delay, 60000);
            const res = yield call(getTaskStatus, payload)
            console.log('status', res);
            if (res.code === 200) {
                yield put({
                    type: 'saveTaskStatus',
                    payload: res.raw.taskStatus
                })
                if (res.raw.taskStatus !== 2 && res.raw.taskStatus !== 3) {
                    yield put({
                        type: 'getTaskStatus',
                        payload: payload
                    })
                } else if (res.raw.taskStatus === 2) {
                    yield put({
                        type: 'saveTaskStatus',
                        payload: res.raw.taskStatus
                    })
                    yield put({
                        type: 'saveLoading',
                        payload: false
                    })
                } else if (res.raw.taskStatus === 3) {
                    message.error('任务异常')
                }
            }
        },

        * downLoadMaps({ payload }, { put, call }) {
            const res = yield call(downLoadMaps, payload)
            console.log(res)
            let link = document.createElement('a')
            link.download = 'test'
            link.href = 'http://10.89.101.39:9000/' + res.path
            link.click()
            link.remove()
        },

        * downLoadMine({ payload }, { put, call }) {
            const res = yield call()
        }
    },
    reducers: {
        saveScale(state, {
            payload
        }) {
            return {
                ...state,
                scale: payload
            }
        },
        saveTaskId(state, {
            payload
        }) {
            return {
                ...state,
                taskId: payload
            }
        },
        saveLoading(state, {
            payload
        }) {
            return {
                ...state,
                loading: payload
            }
        },
        saveCurrent(state, {
            payload
        }) {
            return {
                ...state,
                current: payload
            }
        },
        saveTaskStatus(state, {
            payload
        }) {
            return {
                ...state,
                taskStatus: payload
            }
        }
    }
}
export default Model